package estyle.auto.nkornilova.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: nkornilova
 * Date: 07.05.13
 * Time: 17:04
 * To change this template use File | Settings | File Templates.
 */
public class BankFilterPage extends AnyPage {

    public BankFilterPage(PageManager pages) {
        super(pages);
    }

    @FindBy(id = "numTd")
    private WebElement bicField;

    @FindBy(id = "ksnp")
    private WebElement numberAccountField;

    @FindBy(id = "regn")
    private WebElement regNumField;

    @FindBy(id = "namep")
    private WebElement fullNameField;

    @FindBy(xpath = "//span[text()=\"Выбрать\"]/..")
    private WebElement submitButton;


    public BankFilterPage setBicField(String filterText) {
        bicField.sendKeys(filterText);
        return this;
    }

    public BankFilterPage setNumberField(String filterText) {
        numberAccountField.sendKeys(filterText);
        return this;
    }

    public BankFilterPage setRegNumField(String filterText) {
        regNumField.sendKeys(filterText);
        return this;
    }

    public BankFilterPage setFullNameField(String filterText) {
        fullNameField.sendKeys(filterText);
        return this;
    }

    public BankFilterPage sendKeyEnterForSearch(String endFieldName)
    {
        if(endFieldName.equals("РЕГНОМЕР"))
            pressEnterIntoWebElement(regNumField);
        return this;
    }

    public BankFilterPage moveToInnerFrame()
    {
        moveToFrameByLocator(By.xpath("//iframe[@src='/fbsWeb/dictionary/bic_choose_dialog.do?code=main']"));
        return this;
    }

    public BankFilterPage selectBankRecordOne()
    {
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        List<WebElement> selectGridRecords = driver.findElements(By.xpath("//div[@class='datagrid-body']//tr//td[@field='num']"));
        if(selectGridRecords!=null && !selectGridRecords.isEmpty())
        {
            selectGridRecords.get(0).click();
        }
        return this;
    }

    public MessagePage clickOnChooseButton()
    {
        submitButton.click();
        return pages.messagePage;
    }

    public BankFilterPage moveToOut()
    {
        driver.switchTo().defaultContent();
        moveToFrameByLocator(By.xpath("//iframe[@id='appPopupFrame']"));
        return this;
    }
}
